let title = ''
const app = getApp()
const db = wx.cloud.database()
const p_db = db.collection('publish')

Page({

    /**
     * 页面的初始数据
     */
    data: {
        no_scroll: true,
        shoucangUrl: "cloud://cloud1-7gp73os4feb4632b.636c-cloud1-7gp73os4feb4632b-1315259068/icon/我的页面_icon/我的喜欢_未选中.png",
        shoucangtext: "点赞",
        subscribetext: "关注",
        show: '',
        subscribe: '',
        article: [],
        collector: {},
        userInfo: {},
        mylogin_users: [],
        time: '',
        collectornum: null,
        yes: 'yes'
    },
    onLoad(options) {
        console.log('onload')
        p_db
            .get()
            .then(res => {
                this.setData({
                    yes: res.data[0].yes
                })
            })
        const userInfo = wx.getStorageSync('userInfo');
        console.log(userInfo, !!userInfo);
        this.setData({
            hasUserInfo: !!userInfo,
            userInfo: userInfo,
        })
        // app.globalData.userInfo = userInfo
        var openid = wx.getStorageSync('openid')
        // var like = wx.getStorageSync('like')
        let id = options.id
        wx.cloud.database().collection('articlelist')
            .doc(id)
            .get()
            .then(res => {
                console.log(res.data)
                console.log(res.data.collector.length)
                this.setData({
                    article: res.data,
                    collectornum: res.data.collector.length
                })
                var collectorlist = this.data.article.collector
                if (collectorlist.indexOf(openid) > -1) {
                    this.setData({
                        shoucangUrl: "cloud://cloud1-7gp73os4feb4632b.636c-cloud1-7gp73os4feb4632b-1315259068/icon/我的页面_icon/我的喜欢.png",
                        show: 1,
                        shoucangtext: "取消点赞"
                    })
                    console.log('点赞存在')
                } else {
                    this.setData({
                        shoucangUrl: "cloud://cloud1-7gp73os4feb4632b.636c-cloud1-7gp73os4feb4632b-1315259068/icon/我的页面_icon/我的喜欢_未选中.png",
                        show: 0,
                        shoucangtext: "点赞"
                    })
                    console.log('点赞不存在')
                }
            })
            .catch(err => {
                console.log('请求失败', err)
            })
        wx.cloud.database().collection('login_users').where({
                _openid: openid,
            }).get()
            .then(res => {
                this.setData({
                    mylogin_users: res.data
                })
                console.log(this.data.mylogin_users[0].mysubscribe)
                var mysubscribelist = this.data.mylogin_users[0].mysubscribe
                if (mysubscribelist.indexOf(this.data.article._openid) > -1) {
                    this.setData({
                        //   shoucangUrl: "cloud://cloud1-7gp73os4feb4632b.636c-cloud1-7gp73os4feb4632b-1315259068/icon/收藏_选中.png",
                        subscribe: 1,
                        subscribetext: "已关注"
                    })
                    console.log('关注存在')
                } else {
                    this.setData({
                        //   shoucangUrl: "cloud://cloud1-7gp73os4feb4632b.636c-cloud1-7gp73os4feb4632b-1315259068/icon/收藏_未选中.png",
                        subscribe: 0,
                        subscribetext: "关注"
                    })
                    console.log('关注不存在')
                }
            })
    },

    onShow() {
        console.log(app.globalData.userInfo)
        this.setData({
            userInfo: app.globalData.userInfo
        })
    },
    preview(event) {
        let currentUrl = event.target.dataset.src
        wx.previewImage({
            current: currentUrl, // 当前显示图片的http链接
            urls: [currentUrl] // 需要预览的图片http链接列表
        })
    },
    goTop: function (e) {
        if (wx.pageScrollTo) {
            wx.pageScrollTo({
                scrollTop: 0
            })
        } else {
            wx.showModal({
                title: '提示',
                content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
            })
        }
    },
    onPageScroll: function (e) {
        if (e.scrollTop > 200) {
            this.setData({
                no_scroll: false
            });
        } else {
            this.setData({
                no_scroll: true
            });
        }
    },


    /**
     * 生命周期函数--监听页面加载
     */

    subscribe() {
        const userInfo = wx.getStorageSync('userInfo');
        // app.globalData.userInfo = userInfo
        var openid = wx.getStorageSync('openid')
        var authoropenid = this.data.article._openid
        console.log(authoropenid)
        console.log(this.data.article._openid)
        if (app.globalData.userInfo) {
            if (openid == this.data.article._openid) {
                wx.showToast({
                    title: '不可关注自己~',
                    icon: 'none'
                })

            } else {
                if (this.data.subscribe == 0) {
                    wx.cloud.database().collection('login_users').where({
                        _openid: openid
                    }).update({
                        data: {
                            mysubscribe: wx.cloud.database().command.push(authoropenid)
                        }
                    })
                    this.setData({
                        subscribe: 1,
                        subscribetext: '已关注'
                    })
                    wx.showToast({
                        title: '关注成功',
                        duration: 1000 //持续的时间
                    })
                    console.log("关注成功")
                } else {
                    wx.cloud.database().collection('login_users').where({
                        _openid: openid
                    }).update({
                        data: {
                            mysubscribe: wx.cloud.database().command.pull(authoropenid)
                        }
                    })
                    this.setData({
                        subscribe: 0,
                        subscribetext: '关注'
                    })
                    wx.showToast({
                        title: '已取消关注',
                        duration: 1000 //持续的时间
                    })
                    console.log("取消关注成功")

                }
            }
        } else {
            wx.showToast({
                title: '请登录',
                icon: 'none'
            })
        }
    },

    forcollect() {
        if (app.globalData.userInfo) {

            const userInfo = wx.getStorageSync('userInfo');
            // app.globalData.userInfo = userInfo
            var openid = wx.getStorageSync('openid')
            var like = wx.getStorageSync('like')
            console.log(openid)
            console.log(like)
            if (this.data.show == 0) {
                var articleid = this.data.article._id
                console.log(articleid)
                wx.cloud.database().collection('articlelist').doc(articleid).update({
                    data: {
                        collector: wx.cloud.database().command.push(openid)
                    }
                })
                this.setData({
                    collectornum:this.data.collectornum+1,
                    shoucangUrl: "cloud://cloud1-7gp73os4feb4632b.636c-cloud1-7gp73os4feb4632b-1315259068/icon/我的页面_icon/我的喜欢.png",
                    show: 1,
                    shoucangtext: "取消"
                })
            } else {

                var articleid = this.data.article._id
                wx.cloud.database().collection('articlelist').doc(articleid).update({
                    data: {
                        collector: wx.cloud.database().command.pull(openid)
                    }
                })

                this.setData({
                    collectornum:this.data.collectornum-1,
                    shoucangUrl: "cloud://cloud1-7gp73os4feb4632b.636c-cloud1-7gp73os4feb4632b-1315259068/icon/我的页面_icon/我的喜欢_未选中.png",
                    show: 0,
                    shoucangtext: "收藏"
                })
            }
        } else {
            wx.showToast({
                title: '请登录',
                icon: 'none'
            })
        }
    },

    title_input(e) {

        title = e.detail.value

        console.log(title)
        this.setData({
            inputValue: title
        })
    },

    submit(e) {
        console.log(e)
        if (title == '') {
            wx.showToast({
                title: '请输入回复内容',
                icon: 'none'
            })
        } else {
            wx.showLoading({
                title: '发送中...',
            })
            var date = new Date();

            var Y = date.getFullYear();

            var M = date.getMonth() + 1;

            var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();

            var H = date.getHours()

            var min = date.getMinutes()

            this.setData({
                time: M + "-" + D + " " + H + ":" + min
            })
            const userInfo = wx.getStorageSync('userInfo');
            // app.globalData.userInfo = userInfo
            var openid = wx.getStorageSync('openid')
            // let myname = app.globalData.userInfo.nickName

            if (app.globalData.userInfo) {
                var articleid = this.data.article._id
                console.log(articleid)
                wx.cloud.database().collection('articlelist').doc(articleid).update({
                    data: {
                        comments: wx.cloud.database().command.push({
                            avatarUrl: userInfo.avatarUrl, //回复者头像
                            nickName: userInfo.nickName, //回复者名称
                            _id: this.pagesData, //动态ID,随机数
                            commentText: title, //回复内容
                            commentTime: this.data.time, //回复时间
                            openid: openid, //回复者openid
                            replyName: '', //被回复者名称
                            isbuilding: true, //是否为楼主
                            childComment: [], //子评论
                            id: Math.floor(Math.random() * 100000000) //通过该条记录在父评论数组中的索引来添加子评论
                        })
                    },
                })

                wx.showToast({
                    title: '评论成功！',
                    icon: 'success',
                    duration: 1000 //持续的时间
                })
                this.data.article.comments.push({
                    avatarUrl: userInfo.avatarUrl, //回复者头像
                    nickName: userInfo.nickName, //回复者名称
                    _id: this.pagesData, //动态ID,随机数
                    commentText: title, //回复内容
                    commentTime: this.data.time, //回复时间
                    openid: openid, //回复者openid
                    replyName: '', //被回复者名称
                    isbuilding: true, //是否为楼主
                    childComment: [], //子评论
                    id: Math.floor(Math.random() * 100000000) //通过该条记录在父评论数组中的索引来添加子评论
                })
                console.log(this.data.article.comments)
                this.setData({
                    title: ''
                })
                //   wx.hideLoading({
                //     success: (res) => {},
                //   })
                this.onLoad(); // 调用页面的onLoad()方法进行刷新页面
            } else {
                wx.showToast({
                    title: '请登录',
                    icon: 'none'
                })
            }
        }
    },

    onPullDownRefresh: function () { // 下拉刷新
        var page = getCurrentPages().pop(); // 得到这个页面对象
        page.onLoad(); // 调用页面的onLoad()方法进行刷新页面
        wx.stopPullDownRefresh() // 刷新成功后停止下拉刷新
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () { // 下拉刷新
        var page = getCurrentPages().pop(); // 得到这个页面对象
        page.onLoad(); // 调用页面的onLoad()方法进行刷新页面
        wx.stopPullDownRefresh() // 刷新成功后停止下拉刷新
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})